package com.paomi.client.fragment.first.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.paomi.client.MyApplication;
import com.paomi.client.R;
import com.paomi.client.fragment.first.model.ListTopJson;
import com.paomi.client.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by pc on 2018/1/10.
 */

public class TopAdapter extends RecyclerView.Adapter {

    private Context context;
    public List<ListTopJson> smartList=new ArrayList<>();
    private ClickListener mListener;
    private View view;

    public interface ClickListener{
        public void clickTopListener(ListTopJson smart);
    }

    public TopAdapter(Context context, ClickListener mListener){
        this.context=context;
        this.mListener=mListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.top_list_item, null);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewholder=(ViewHolder)holder;
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) viewholder.ll_ome.getLayoutParams();
        params.width = (int) ((DeviceUtils.getScreenWidth() -DeviceUtils.dipToPX(32)) * 0.5);
        viewholder.ll_ome.setLayoutParams(params);
        ListTopJson smart=smartList.get(position);
        viewholder.textview_one.setText(smart.getType_name());
        if(smart.getType_name().equals("精彩演出")){
            viewholder.view_one.setImageResource(R.mipmap.img_s_yanchu_di);
        }else{
            viewholder.view_one.setImageResource(R.mipmap.img_s_zixun_di);
        }
        if (smart.getType()==0){
            viewholder.view_one.setVisibility(View.GONE);
        }else{
            viewholder.view_one.setVisibility(View.VISIBLE);
        }
        viewholder.ll_fu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i=0;i<smartList.size();i++){
                    if (i==position){
                        smartList.get(i).setType(1);
                    }else{
                        smartList.get(i).setType(0);
                    }
                }
                notifyDataSetChanged();
                mListener.clickTopListener(smartList.get(position));
            }
        });

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return smartList.size();
    }

    //手写的获得视图的方法
    public ListTopJson getItem(int position){
        return smartList.get(position);

    }
    //手写的重置数据的方法
    public void setdata(List<ListTopJson> list){
        if (list!=null&&list.size()>0){
            smartList.addAll(list);
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView view_one;
        TextView textview_one;
        LinearLayout ll_fu;
        RelativeLayout ll_ome;


        public ViewHolder(View itemView) {
            super(itemView);
            view_one=itemView.findViewById(R.id.view_one);
            textview_one=(TextView) itemView.findViewById(R.id.textview_one);
            ll_fu= (LinearLayout) itemView.findViewById(R.id.ll_fu);
            ll_ome= (RelativeLayout) itemView.findViewById(R.id.rl_order_one);
        }
    }
}

